// import { createStore } from "redux";
import { createStore, applyMiddleware } from "./redux";


// import { createStore, applyMiddleware } from "redux";
// import thunk from "redux-thunk"
// import logger from "redux-logger"

// 定义修改规则
function countReducer(state = 0, action) {
    switch (action.type) {
      case "ADD":
        return state + 1;
      case "MINUS":
        return state - action.payload || 1;
      default:
        return state;
    }
  }

// 创建一个数据仓库
//  const store = createStore(countReducer)
 const store = createStore(countReducer, applyMiddleware(thunk, logger))
 export default store;



 // 中间件logger
function logger({dispatch, getState}) {
  return (next) => (action) => {
    console.log("logger next", next);
    console.log("---------------------------------");
    console.log(action.type + "执行啦");
    const prevState = getState();
    console.log("prev state", prevState);

    const returnValue = next(action);
    const nextState = getState();

    console.log("next state", nextState);
    console.log("---------------------------------");
    return returnValue;
  };
}

// thunk 判断action，如果是函数，就执行函数
function thunk({dispatch, getState}) {
  return (next) => (action) => {
    console.log("thunk next", next);

    if (typeof action === "function") {
      return action(dispatch, getState);
    }
    return next(action);
  };
}